# -*- coding: utf-8 -*-
#handle music


import pygame
import os

data_dir = 'data'
music_dir = os.path.join(data_dir, 'music')
sound_dir = os.path.join(data_dir, 'sound')

#sound_cannel = pygame.mixer.Channel(0)

sounds = {}

def play_music(song,loop = -1, pos = 0.0):
    """Play a song!
    arguments: 
    song (ogg format)
    loops, -1 is neverending, default -1
    pos, where to start, default 0.0"""
    
    #songname = os.path.basename(song) + '.ogg'
    
    fullname = os.path.join(music_dir, song)
    try:
        pygame.mixer.music.load(fullname)

    except pygame.error, message:
        print "Could not load song", fullname
        raise SystemExit, message
    
    pygame.mixer.music.play(loop, pos)
    
def stop_music(fadeout = 0):
    """stops the music currently being played
    arguments: fadeout, default = 0"""
    if fadeout > 0:
        pygame.mixer.music.fadeout(fadeout)
    else:
        pygame.mixer.music.stop()
        
def play_sound(filename):
    """Play single soundeffect"""
    global sounds

    if not sounds.has_key(filename):
        path = os.path.join(sound_dir, filename)
        sounds[filename] = pygame.mixer.Sound(path)
    sounds[filename].play()
        
